package com.thread.learn.thread5;

public class Singleton2 {
	
	//私有化构造方法
	private Singleton2() {}
	
	//防止指令重排序问题
	private static volatile Singleton2 instance;
	
	/*public static synchronized Singleton2 getInstance() {
		if(instance==null) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			instance =new Singleton2();
		}
		return instance;
	}*/
	
	/**
	 * 双重检查锁加锁
	 * @return
	 */
	public static synchronized Singleton2 getInstance() {
		if(instance==null) {
			synchronized (Singleton2.class) {
				if(instance==null) {
					instance =new Singleton2();//指令重排序
				}
			}
		}
		return instance;
	}

}
